import { loginUsingPost } from '@/services/data-factory/dengluxiaoyanjiekou';
import { Button, Form, Input, message } from 'antd';
import React from 'react';
import styles from './index.less';

interface LoginParams {
  account: string;
  password: string;
}

const LoginPage: React.FC = () => {
  const handleSubmit = async (values: LoginParams) => {
    try {
      const res = await loginUsingPost({
        account: values.account,
        password: values.password,
      });

      if (res.code === 100200 && res.data) {
        localStorage.setItem('token', res.data.toString());
        localStorage.setItem('account', values.account);
        message.success('登录成功');
        window.location.href = '/home';
      } else {
        message.error(res.msg || '登录失败');
      }
    } catch (error) {
      message.error('登录失败，请检查网络连接');
    } finally {
    }
  };

  return (
    <div className={styles.container}>
      <div className={styles.content}>
        <div className={styles.title}>数据工厂管理系统</div>
        <Form
          name="login"
          onFinish={handleSubmit}
          autoComplete="off"
          className={styles.form}
        >
          <Form.Item
            name="account"
            rules={[{ required: true, message: '请输入账号' }]}
          >
            <Input placeholder="账号" size="large" />
          </Form.Item>

          <Form.Item
            name="password"
            rules={[{ required: true, message: '请输入密码' }]}
          >
            <Input.Password placeholder="密码" size="large" />
          </Form.Item>

          <Form.Item>
            <Button type="primary" htmlType="submit" block size="large">
              登录
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
};

export default LoginPage;
